using System;
using System.Windows.Forms;
using Jbe.TestSuite.Infrastructure.Interface;
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.Commands;
using Jbe.CABExtension.WinForms;
using Jbe.TestSuite.Help.Constants;
using Jbe.TestSuite.Help.Interface.Services;
using Jbe.TestSuite.Help.Services;
using Jbe.TestSuite.Help.Properties;
using System.Drawing;
using Jbe.CABExtension.WinForms.Workspaces;
using Jbe.CABExtension.SmartPartInfos;
using System.Reflection;
using Jbe.TestSuite.Infrastructure.Interface.Services;

namespace Jbe.TestSuite.Help
{
    public class ModuleController : WorkItemController
    {
        private IDynamicHelp dynamicHelp;
        private HelpView helpView;
        private IUIElementCreationService uiElementCreationService;

        [ServiceDependency]
        public IUIElementCreationService UIElementCreationService
        {
            set { uiElementCreationService = value; }
        }

        public override void Run()
        {
            dynamicHelp = WorkItem.RootWorkItem.Services.AddNew<DynamicHelpService, IDynamicHelp>();

            IMenuItem menuItem = uiElementCreationService.CreateMenuItem("License");
            WorkItem.UIExtensionSites[UIExtensionSiteNames.HelpMenuItem].Add(menuItem);
            menuItem.Click += new EventHandler(LicenseMenuItemClick);

            menuItem = uiElementCreationService.CreateMenuItem("Credits");
            WorkItem.UIExtensionSites[UIExtensionSiteNames.HelpMenuItem].Add(menuItem);
            menuItem.Click += new EventHandler(CreditsMenuItemClick);

            menuItem = uiElementCreationService.CreateMenuItem("Contents", Resources.Help.ToBitmap());
            WorkItem.UIExtensionSites[UIExtensionSiteNames.HelpMenuItem].Add(menuItem);
            menuItem.Click += new EventHandler(ContentsMenuItemClick);

            ShowHelpView();
            dynamicHelp.ShowHelp(Assembly.GetExecutingAssembly(), "Help/Contents.htm");
        }

        private void ShowHelpView()
        {
            if (helpView == null)
            {
                DockPanelSmartPartInfo dpInfo = new DockPanelSmartPartInfo("Help", "Help");
                dpInfo.DockingType = DockingType.TaskView;
                dpInfo.Icon = Resources.Help;
                helpView = WorkItem.SmartParts.AddNew<HelpView>();
                WorkItem.Workspaces[WorkspaceNames.DockPanelWorkspace].Show(helpView, dpInfo);
                helpView.Disposed += new EventHandler(HelpViewDisposed);
            }
            WorkItem.Workspaces[WorkspaceNames.DockPanelWorkspace].Activate(helpView);
        }

        #region Command and Event handler
        
        private void LicenseMenuItemClick(object sender, EventArgs e)
        {
            ShowHelpView();
            dynamicHelp.ShowHelp(Assembly.GetExecutingAssembly(), "Help/License.htm");
        }

        private void CreditsMenuItemClick(object sender, EventArgs e)
        {
            ShowHelpView();
            dynamicHelp.ShowHelp(Assembly.GetExecutingAssembly(), "Help/Credits.htm");
        }

        private void ContentsMenuItemClick(object sender, EventArgs e)
        {
            ShowHelpView();
            dynamicHelp.ShowHelp(Assembly.GetExecutingAssembly(), "Help/Contents.htm");
        }

        private void HelpViewDisposed(object sender, EventArgs e)
        {
            WorkItem.SmartParts.Remove(sender);
            helpView = null;
        }
        
        #endregion
    }
}
